package com.three.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.three.entity.Announce;
import com.three.service.AnnounceService;
import com.three.util.JPushUtil;

@Controller
@RequestMapping("/announce")
public class AnnounceController {

	@Autowired
	AnnounceService announceService;
	private static Logger logger = Logger.getLogger(AnnounceController.class); 
	
	@RequestMapping(value="/findAnnounces",
			method = RequestMethod.GET)
	@ResponseBody
	public List<Announce> findAnnounces(Integer cid) {
		return announceService.findAnnouncesByCid(cid);
	} 
	
	@RequestMapping("/addAnnounce")
	@ResponseBody
	public void addAnnounce(@RequestBody Announce ann,
			HttpServletResponse response) throws IOException {
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		String result = announceService.addAnnounce(ann); 
		out.write(result);
		out.close();
//		if ("success".equals(result)) {			
			JPushUtil.createPushAnnounceMsg(String.valueOf(ann.getCid()),
					ann.getTitle(), "【课堂派】您有一条班级公告");
//		}
	} 
	
	@RequestMapping("/editAnnounce")
	@ResponseBody
	public void editAnnounce(String title,
			String content,
			Integer aid,
			Integer cid,
			PrintWriter out){
		String result = announceService.editAnnounce(aid,title,content); 
		out.write(result);
		out.close();
//		if ("success".equals(result)) {	
			JPushUtil.createPushAnnounceMsg(String.valueOf(cid),
					title, "【课堂派】您有一条班级公告(更新)");
//		}
	} 
	
	@RequestMapping("/deleteAnnounce")
	@ResponseBody
	public void deleteAnnounce(Integer aid,
			PrintWriter out){
		String result = announceService.deleteAnnounce(aid); 
		out.write(result);
		out.close();
	} 
	
}
